# Maintainer: Muflone http://www.muflone.com/contacts/english/
# Contributor: Arne Hoch <arne@derhoch.de>

pkgname=dbeaver
pkgver=23.2.2
pkgrel=1
pkgdesc="Free universal SQL Client for developers and database administrators (community edition)"
arch=('loong64' 'x86_64')
url="https://dbeaver.io/"
license=("Apache")
depends=('java-runtime>=17' 'gtk3' 'gtk-update-icon-cache' 'libsecret')
makedepends=('maven' 'java-runtime=17')
optdepends=('dbeaver-plugin-office: export data in Microsoft Office Excel format'
            'dbeaver-plugin-svg-format: save diagrams in SVG format')
conflicts=('dbeaver-plugin-sshj-lib')
replaces=('dbeaver-plugin-sshj-lib')
source=("${pkgname}-${pkgver}.tar.gz"::"https://github.com/dbeaver/dbeaver/archive/${pkgver}.tar.gz"
        "io.${pkgname}.DBeaver.desktop"
        "${pkgname}.sh"
        "${pkgname}.profile.gz"
        "${pkgname}.hook"
        "${pkgname}.install")
sha256sums=('7bea1833a26e928e43f8dc977b8a09949b7b1c63959723e5c26d87e24a26dbdb'
            '9480a7d08f680e10c399db070c5a04cbabf282442602a2ef83d1159fe7c3e88b'
            '406a2980806c394670e88b1ae70134900be376c2ea4a4216610591cc8b557526'
            '1863e74bdcf22b7328e6e8487cbebff7d5360e34bde85c1dd226b168b4737034'
            'f8b763ca210bfa4d9a4e407b656ba4f5d1bf2f3f54c67044f7a4dd0c3625fc22'
            'f8d65dd933049b587a5815ea75a30ef944300b812df383ca1c2dcd68280bc7ab')
install="${pkgname}.install"

prepare() {
  # Fix version number in profile file
  gzip --decompress --keep --stdout "${pkgname}.profile.gz" | 
    sed "s/DBEAVER_VERSION/${pkgver}/g" |
    gzip -9 > "${pkgname}.profile-${pkgver}.gz"

  # Avoid the use of any Java 18/19/20, actually incompatible with the build
  export JAVA_HOME="/usr/lib/jvm/$(archlinux-java status | tail -n +2 | sort | cut -d ' ' -f 3 | sort -nr -k 2 -t '-' | grep -vE '18-|19-|20-' -m 1)"
  cd "${pkgname}-${pkgver}"

  # Download dependencies during prepare FS#55873
  # https://bugs.archlinux.org/task/55873
  export MAVEN_OPTS="-Xmx2048m"
  mvn --batch-mode validate
}

build() {
  cd "${pkgname}-${pkgver}"
  mvn --batch-mode package
}

package() {
  cd "${pkgname}-${pkgver}/product/community"
  # Install icons into /usr/share/icons/hicolor
  for _size in 16 32 48 64 128 256 512
  do
    install -m 644 -D "icons-sources/icon_${_size}x${_size}.png" \
      "${pkgdir}/usr/share/icons/hicolor/${_size}x${_size}/apps/dbeaver.png"
  done

  # Move into the target directory
  cd "target/products/org.jkiss.dbeaver.core.product/linux/gtk/${CARCH}"

  # Initially install everything into /usr/lib/dbeaver
  install -m 755 -d "${pkgdir}/usr/lib"
  cp -r "dbeaver" "${pkgdir}/usr/lib/${pkgname}"

  # Move shared data to /usr/share/dbeaver
  cd "${pkgdir}/usr/lib/${pkgname}"
  install -m 755 -d "${pkgdir}/usr/share/${pkgname}"
  for _file in configuration features p2 .eclipseproduct artifacts.xml dbeaver.ini readme.txt
  do
    mv "${_file}" "${pkgdir}/usr/share/${pkgname}"
    ln -s "/usr/share/${pkgname}/${_file}" .
  done

  # Install additional licenses
  install -m 755 -d "${pkgdir}/usr/share/licenses"
  mv licenses "${pkgdir}/usr/share/licenses/${pkgname}"
  ln -s "/usr/share/licenses/${pkgname}" "${pkgdir}/usr/lib/${pkgname}/licenses"

  # Install icons
  install -m 755 -d "${pkgdir}/usr/share/pixmaps"
  mv dbeaver.png "${pkgdir}/usr/share/pixmaps/${pkgname}.png"
  mv icon.xpm "${pkgdir}/usr/share/pixmaps/${pkgname}.xpm"

  # Install executable script into /usr/bin
  install -m 755 -d "${pkgdir}/usr/bin"
  install -m 755 "${srcdir}/dbeaver.sh" "${pkgdir}/usr/bin/${pkgname}"

  # Install application launcher into /usr/share/applications
  install -m 755 -d "${pkgdir}/usr/share/applications"
  install -m 755 -t "${pkgdir}/usr/share/applications" "${srcdir}/io.${pkgname}.DBeaver.desktop"

  # Clean up and install new profile
  rm -rf "${pkgdir}/usr/share/${pkgname}/p2/org.eclipse.equinox.p2.core"
  cd "${pkgdir}/usr/share/${pkgname}/p2/org.eclipse.equinox.p2.engine/profileRegistry/DefaultProfile.profile"
  find . -name "*.profile.gz" -delete
  install -m 644 "${srcdir}/${pkgname}.profile-${pkgver}.gz" "1502633007017.profile.gz"
  cd "${pkgdir}/usr/share/${pkgname}/p2/org.eclipse.equinox.p2.engine"
  rm -f ".settings/org.eclipse.equinox.p2.artifact.repository.prefs"
  rm ".settings/org.eclipse.equinox.p2.metadata.repository.prefs"
  rmdir ".settings"

  # Install system hook
  install -m 755 -d "${pkgdir}/usr/share/libalpm/hooks"
  install -m 644 "${srcdir}/${pkgname}.hook" "${pkgdir}/usr/share/libalpm/hooks"

  # Create configuration file (handled by the hook)
  cd "${pkgdir}/usr/share/dbeaver/configuration/org.eclipse.equinox.simpleconfigurator"
  install -m 755 -d "${pkgdir}/etc/${pkgname}/bundles.d"
  mv "bundles.info" "${pkgdir}/etc/${pkgname}/bundles.d/00-${pkgname}.info"
  ln -s "/etc/${pkgname}/bundles.info" .
}
